package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.GoodsTypeDao;
import com.qububu.emporium.goodsType.dto.BigTypeDto;
import com.qububu.emporium.goodsType.dto.LittleTypeDto;

/**
 * @功能 商品类型的数据库操作
 * */
@Repository
public class GoodsTypeDaoImpl implements GoodsTypeDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public List<BigTypeDto> getGoodsBigTypes() {
		// TODO Auto-generated method stub
		
		List<BigTypeDto> list = null;
		
		String sql = "select * from goods_big_type";
		
		List<Map<String, Object> > mapList = jdbcTemplate.queryForList(sql);
		
		list = new ArrayList<BigTypeDto>();
		BigTypeDto bt = null;
		for (Map<String, Object> elem : mapList) {
			
			bt = new BigTypeDto(String.valueOf(elem.get("big_id") ));

			bt.setTypeName((String) elem.get("type_name") );
			
			list.add(bt);
		}
		
		return list;
	}
	
	public List<LittleTypeDto> getLittleTypesByBigTypeId(int bigtypeId) {
		// TODO Auto-generated method stub
		
		List<LittleTypeDto> list = null;
		
		String sql = "select * from goods_little_type where big_type_id = ?";
		
		List<Map<String, Object>> elemList = jdbcTemplate.queryForList(sql, bigtypeId);
		
		list = new ArrayList<LittleTypeDto>();
		LittleTypeDto li = null;
		for (Map<String, Object> elem : elemList) {
			li = new LittleTypeDto((Long)elem.get("little_id"), (Long)elem.get("big_type_id"));
			li.setTypeName((String) elem.get("type_name") );
			
			list.add(li);
		}
		
		return list;
	}
}
